package com.transaction.simulate.service.impl;

import com.transaction.common.util.DateUtil;
import com.transaction.simulate.mapper.ChannelMapper;
import com.transaction.simulate.mapper.ElContractMapper;
import com.transaction.simulate.model.dto.Account;
import com.transaction.simulate.model.dto.Channel;
import com.transaction.simulate.model.dto.ElContract;
import com.transaction.simulate.model.vo.ChannelAccountVo;
import com.transaction.simulate.service.ElContractService;
import com.transaction.simulate.util.SequenceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ElContractServiceImpl implements ElContractService{

    private static Logger LOGGER = LoggerFactory.getLogger(ElContractServiceImpl.class);

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private SequenceUtil sequenceUtil;

    @Autowired
    private ElContractMapper elContractMapper;

    @Async
    public void generateChannelElContract(ChannelAccountVo channelAccountVo){
        List<ElContract> elContractList = new ArrayList<>();
        List<Account> accounts = channelAccountVo.getTransAccounts();

        String channelCode = channelAccountVo.getChannelCode();
        String transDate = channelAccountVo.getTransDate();
        Channel channel = channelMapper.selectChannelByChannelCode(channelCode);
        accounts.forEach(v->{
            ElContract elContract = setElContract(v,channelAccountVo,channel,transDate);
            elContractList.add(elContract);
        });
        elContractMapper.generateChannelElContract(elContractList);
    }

    private ElContract setElContract(Account v, ChannelAccountVo channelAccountVo, Channel channel,String transDate) {
        ElContract elContract = new ElContract();

        String channelCode = channelAccountVo.getChannelCode();
        String fundCode = channelAccountVo.getFundCode();
        String creatorCode = channel.getCreatorCode();
        String transActionTime = DateUtil.formatDate(new Date(),DateUtil.FORMAT_STR_TIME6);

        String serialNo = fundCode + "1" + creatorCode + transDate + sequenceUtil.getSixDigitSequence();

        elContract.setChannelCode(channelCode);
        elContract.setFundCode(fundCode);
        elContract.setTransactionAccountId(v.getTransactionAccountId());
        elContract.setSerialNo(serialNo);
        elContract.setDistributorCode(creatorCode);
        elContract.setInvestorName(v.getInvestorName());
        elContract.setIndividualOrInstitution(v.getIndividualOrInstitution());
        elContract.setCertificateType(v.getCertificateType());
        elContract.setCertificateNo(v.getCertificateNo());
        elContract.setTransactionDate(transDate);
        elContract.setTransactionTime(transActionTime);
        elContract.setAlternationDate(transDate);
        return elContract;
    }

    @Override
    public List<Map<String, Object>> getElContractToMap(String channelCode, List<String> dateRange) {
        return elContractMapper.getElContractToMap(channelCode,dateRange);
    }

    @Override
    //TODO
    public List<Map<String, Object>> get31ElContractToMap(String channelCode, List<String> dateRange) {
        return elContractMapper.getElContractToMap(channelCode,dateRange);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertElContract(String channelCode, List<Map<String, Object>> dataList) {
        elContractMapper.deleteElContract(channelCode,dataList);
        elContractMapper.insertElContract(channelCode,dataList);
    }

    @Override
    public void updateElContract(String channelCode, List<Map<String, Object>> dataList) {
        elContractMapper.updateElContract(channelCode,dataList);
    }
}
